package org.htmlparser.visitors;

import org.htmlparser.Tag;
import org.htmlparser.Text;
import org.htmlparser.util.Translate;

public class TextExtractingVisitor extends NodeVisitor
{
  private boolean preTagBeingProcessed;
  private StringBuffer textAccumulator = new StringBuffer();

  public TextExtractingVisitor()
  {
    this.preTagBeingProcessed = false;
  }

  private boolean isPreTag(Tag paramTag)
  {
    return paramTag.getTagName().equals("PRE");
  }

  private String replaceNonBreakingSpaceWithOrdinarySpace(String paramString)
  {
    return paramString.replace(160, ' ');
  }

  public String getExtractedText()
  {
    return this.textAccumulator.toString();
  }

  public void visitEndTag(Tag paramTag)
  {
    if (isPreTag(paramTag))
      this.preTagBeingProcessed = false;
  }

  public void visitStringNode(Text paramText)
  {
    String str = paramText.getText();
    if (!(this.preTagBeingProcessed))
      str = replaceNonBreakingSpaceWithOrdinarySpace(Translate.decode(str));
    this.textAccumulator.append(str);
  }

  public void visitTag(Tag paramTag)
  {
    if (isPreTag(paramTag))
      this.preTagBeingProcessed = true;
  }
}